package com.example.mwavlearndemo.av.ui.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.mwavlearndemo.R;
import com.example.mwavlearndemo.av.ui.base.IActivityInit;
import com.example.mwavlearndemo.av.util.FolderUtils;
import com.example.mwavmedia.audio.PcmDecoder;
import com.example.mwavmedia.audio.PcmEncoder;
import com.example.mwavmedia.audio.PcmPlayer;
import com.example.mwavmedia.audio.PcmRecorder;
import com.example.mwavmedia.test.ICallback;
import com.example.mwavmedia.test.TestCallBack;
import com.example.mwavmedia.test.TestResourceCBActivity;

import java.lang.reflect.InvocationTargetException;

public class PcmActivity extends AppCompatActivity implements View.OnClickListener, ICallback {

    private PcmRecorder pcmRecorder = new PcmRecorder();
    private PcmPlayer pcmPlayer = new PcmPlayer();

    private PcmEncoder pcmEncoder = new PcmEncoder();
    private PcmDecoder pcmDecoder = new PcmDecoder();

    private String path;
    private String aacPath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pcm);


        //开始audio录制
        AppCompatButton audioRecord = findViewById(R.id.audioRecord);
        audioRecord.setOnClickListener(this);
        //结束audio录制
        AppCompatButton audioStopRecord = findViewById(R.id.audioStopRecord);
        audioStopRecord.setOnClickListener(this);

        //开始audio播放
        AppCompatButton audioPlay = findViewById(R.id.audioPlay);
        audioPlay.setOnClickListener(this);
        //结束audio播放
        AppCompatButton audioStopPlay = findViewById(R.id.audioStopPlay);
        audioStopPlay.setOnClickListener(this);

        //开始audio编码
        AppCompatButton audioEncode = findViewById(R.id.audioEncode);
        audioEncode.setOnClickListener(this);
        //结束audio编码
        AppCompatButton audioStopEncode = findViewById(R.id.audioStopEncode);
        audioStopEncode.setOnClickListener(this);

        //开始audio解码
        AppCompatButton audioDecode = findViewById(R.id.audioDecode);
        audioDecode.setOnClickListener(this);
        //结束audio解码
        AppCompatButton audioStopDecode = findViewById(R.id.audioStopDecode);
        audioStopDecode.setOnClickListener(this);

        //结束audio解码
        AppCompatButton testCb = findViewById(R.id.testCallback);
        testCb.setOnClickListener(this);

        path = FolderUtils.getAudioFolderPath(this) + "record.pcm";
        aacPath = FolderUtils.getAudioFolderPath(this) + "encode.aac";
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.audioRecord:
                pcmRecorder.startRecord(path);
                break;
            case R.id.audioStopRecord:
                pcmRecorder.stopRecord();
                break;
            case R.id.audioPlay:
                pcmPlayer.startPlay(path);
                break;
            case R.id.audioStopPlay:
                pcmPlayer.stopPlay();
                break;
            case R.id.audioEncode:
                pcmEncoder.start(aacPath);
                break;
            case R.id.audioStopEncode:
                pcmEncoder.stop();
                break;
            case R.id.audioDecode:
                pcmDecoder.start(aacPath);
                break;
            case R.id.audioStopDecode:
                pcmDecoder.stop();
                break;
            case R.id.testCallback:
                //调整aar中的activity
                TestCallBack testCallBack = new TestCallBack();
                testCallBack.setICallback(this);
                testCallBack.pushActivity(this);
                break;
        }
    }

    @Override
    public void testSucced() {
        Toast.makeText(this, "接收到aar消息回调测试", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 100){
            int result = data.getIntExtra("result",-1);

            Toast.makeText(this, result == 0 ? "成功":"失败", Toast.LENGTH_SHORT).show();
        }

    }
}